package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.entity.TlCar;
import com.qf.entity.TlCity;
import com.qf.mapper.CarMapper;
import com.qf.mapper.CityMapper;
import com.qf.service.CarService;
import com.qf.utils.Mycode;
import com.qf.utils.ResultMsg;
import com.qf.vo.CenterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class CarServiceImpl extends ServiceImpl<CarMapper, TlCar> implements CarService {

    @Autowired(required = false)
    CityMapper cityMapper;

    @Autowired(required = false)
    CarMapper carMapper;
    /**
     * 主页面查询车次
     * @param carStartaddr 出发地
     * @param carEndaddr 目的地
     * @return
     */
    @Override
    public ResultMsg findByStarAndEnd(String carStartaddr, String carEndaddr) {
        QueryWrapper<TlCar> wrapper = new QueryWrapper<>();
        wrapper.eq("car_startaddr",carStartaddr);
        wrapper.eq("car_endaddr",carEndaddr);
        List<TlCar> list = super.list(wrapper);
        if (list!=null ){
            return ResultMsg.SUCCESS(list);
        }
        return ResultMsg.ERROR(Mycode.CARFIND_ERRO);
    }

    /**
     * 车次信息查询
     * @param carId 车次的id
     * @return 车次详情
     */
    @Override
    public ResultMsg findByCid(int carId) {
        TlCar byId = super.getById(carId);
        if (byId!=null)
            return ResultMsg.SUCCESS(byId);
        return ResultMsg.ERROR(Mycode.CARFIND_ERRO);
    }

    /**
     * 查询车站大屏信息
     * @param cityName 城市名字
     * @return
     */
    @Override
    public ResultMsg findAll(String cityName) {
        List<Integer> byCityName = carMapper.findByCityName(cityName);
        List<TlCar> list = super.listByIds(byCityName);
        if (list!=null)
            return ResultMsg.SUCCESS(list);
        return ResultMsg.ERROR(Mycode.CARFIND_ERRO);
    }

    /**
     * 车站大屏查询
     * @param cityName 城市名字
     * @param carName 车次名字
     * @return
     */
    @Override
    public ResultMsg findAllByCityNameAndCarName(String cityName, String carName) {
        List<Integer> byCityName = carMapper.findByCityName(cityName);
        List<TlCar> list = super.listByIds(byCityName);
        for (TlCar tlCar : list) {
            if (carName.equals(tlCar.getCarName()))
                return ResultMsg.SUCCESS(tlCar);
        }
        return ResultMsg.ERROR(Mycode.CARFIND_ERRO);
    }

    /**
     * 车站大屏信息详情查询
     * @param carName 车次名称
     * @return 起始时间,到达时间,途经城市集合
     */
    @Override
    public ResultMsg findByName(String carName) {
        List<TlCity> byName = cityMapper.findByName(carName);
        TlCar oneByName = carMapper.findOneByName(carName);
        if (byName!=null && oneByName !=null)
            return ResultMsg.SUCCESS(new CenterVo(oneByName.getCarStarttime(),oneByName.getCarEndtime(),byName));
        return ResultMsg.ERROR(Mycode.CARFIND_ERRO);
    }

    /**
     * 主页信息挂载
     * @return 起始站信息
     */

    @Override
    public ResultMsg findAllStarName() {
        List<TlCar> list = super.list();
        Set<String> startaddr = new HashSet<>();
        for (TlCar tlCar : list) {
            startaddr.add(tlCar.getCarStartaddr());
        }
        return ResultMsg.SUCCESS(startaddr);
    }
    /**
     * 主页信息挂载
     * @return 终点站信息
     */
    @Override
    public ResultMsg findAllEndName() {
        List<TlCar> list = super.list();
        Set<String> endaddr = new HashSet<>();
        for (TlCar tlCar : list) {
            endaddr.add(tlCar.getCarEndaddr());
        }
        return ResultMsg.SUCCESS(endaddr);
    }

    /**
     * 
     * @param carStartaddr   发车地
     * @param carEndaddr    目的地
     * @param date    日期
     * @return
     */
    @Override
    public ResultMsg findByStarttime(String carStartaddr, String carEndaddr, Date date) {
        List<TlCar> car = carMapper.findByStarttime(carStartaddr, carEndaddr, date);
        return ResultMsg.SUCCESS(car);
    }


}
